<?php 
class NewsPage extends Page {
    public static $db = array(
		'YoutubeUser' => 'Varchar',
		'YoutubePlayList' => 'Varchar',
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->removeFieldFromTab("Root.Content.Main","Content");
		
		$fields->addFieldToTab("Root.Content.Main", new TextField("YoutubeUser", "Youtube username"));
		$fields->addFieldToTab("Root.Content.Main", new TextField("YoutubePlayList", "Youtube Playlist ID"));
		
		return $fields;
	}
	
	public function LatestVideos($limit = 2) {
		Requirements::javascript( "youtubeservice/javascript/prototype.js" );
		Requirements::javascript( "youtubeservice/javascript/effects.js" );
		Requirements::javascript( "youtubeservice/javascript/lightwindow.js" );
		
		Requirements::css("youtubeservice/css/lightwindow.css");
		
		$youtube = new YoutubeService();
		$playlist_arr = explode(',', $this->YoutubePlayList);
		
		$output = new DataObjectSet();
		foreach($playlist_arr as $playlist) {
			
			$videos = $youtube->getPlaylist(trim($playlist));
			if ($videos instanceof DataObjectSet) {
				foreach($videos as $video) {
					$videoId = array_pop(explode("/", $video->id));	
					
					$output->push(new ArrayData(array(
						"Title" => $video->getField('Title'),
						"Link" => $video->getField('PlayerURL'),
						"Width" => $video->getField('PlayerWidth'),
						"Height" => $video->getField('PlayerHeight'),
						"Image" => $video->getField('SmallThumbnail')->getField('URL'),
						"Duration" => $video->getField('RuntimeMin')
					)));
					continue;
				}	
			}
						
			
		}
		return $output;
	}
}

class NewsPage_Controller extends Page_Controller {
    
	    
}
?>
